package cn.daichi.rabbitdemo.helloword;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.messaging.Message;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class HelloWorldConsumer {

    @RabbitListener(queues = HelloWorldConfig.HELLO_WORLD_QUEUE_NAME)
    public void receive(Message msg, Channel channel) throws IOException {
        System.out.println("receive = " + msg.getPayload());
        channel.basicAck(((Long) msg.getHeaders().get(AmqpHeaders.DELIVERY_TAG)), true);
    }

    @RabbitListener(queues = HelloWorldConfig.HELLO_WORLD_QUEUE_NAME, concurrency = "10")
    public void receive2(Message msg, Channel channel) throws IOException {
        System.out.println("receive2 = " + msg.getPayload() + "------->" + Thread.currentThread().getName());
        channel.basicReject(((Long) msg.getHeaders().get(AmqpHeaders.DELIVERY_TAG)), true);
    }

}
